package work.coffee.connect;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import javax.swing.JOptionPane;

public class PropDb {
	public static Properties loadProperties(String propertiesFile){
		BufferedInputStream bis = null;
		Properties prop = null;
		
		try {
			bis = new BufferedInputStream(new FileInputStream(propertiesFile));
			prop = new Properties();
			prop.loadFromXML(bis);
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "파일이 없습니다.");
		} catch (InvalidPropertiesFormatException e) {
			JOptionPane.showMessageDialog(null, "파일이 유효하지 않습니다.");
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "입력오류");
		}finally{
			if(bis!=null)try {bis.close();} catch (IOException e) {bis=null;}
		}
		return prop;	
	}
	
	
	/*
	public static void saveProperties(String propertiesFile, String...value){
		BufferedOutputStream bos = null;
		Properties prop = null;
		
		try {
			bos = new BufferedOutputStream(new FileOutputStream(propertiesFile));
			prop = new Properties();
			prop.setProperty("DRIVER", value[0]);
			prop.setProperty("URL", value[1]);
			prop.setProperty("USER", value[2]);
			prop.setProperty("PASSWORD", value[3]);
			
			prop.storeToXML(bos, "Database coonnection xml property file");
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "파일이 없습니다.");
		}catch (IOException e) {
			JOptionPane.showMessageDialog(null, "입력오류");
		} finally{
			if(bos!=null)try {bos.close();} catch (IOException e) {bos=null;}
		}
	}
	public static void main(String[] args) {
		saveProperties("dbcon.xml", "com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/mysql_study","root","1111");
	}
	
	public static void prnProperties(Properties prop){
		Set<Entry<Object, Object>> setmap = prop.entrySet();
		
		for (Entry<Object, Object> entry : setmap) {
			System.out.println();
		}
	}*/
}
